﻿using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Xml;
using Interfaces.FeedConverters;
using Interfaces.Filters;
using Interfaces.Models;
using Interfaces.Providers;
using HelperTools;
using NewsDataProvider.FeedConverter;

namespace NewsDataProvider
{
    public class RssNewsDataProvider : INewsDataProvider<IFeed,IFilter>
    {
        public IFeed GetData(string url, IFilter filter)
        {

            var xmlReader = XmlReader.Create(url);
            var newsFeed = SyndicationFeed.Load(xmlReader);
            IFeedConverter<SyndicationFeed, IFeed> converter = new SyndicationFeedConverter(); 
            if (newsFeed != null)
            {
                newsFeed.Items = filter.With(x => x.Filtrate(newsFeed.Items)) as IEnumerable<SyndicationItem>;
                return converter.Convert(newsFeed);
            }
            return null;
        }
    }
}
